package com.ctgu.chatapp.view.fragment.MainFragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.navGraphViewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.ctgu.chatapp.R
import com.ctgu.chatapp.databinding.FragmentSearchBinding
import com.ctgu.chatapp.view.adapter.SearchFriendAdapter
import com.ctgu.chatapp.viewModel.MainViewModel

class Search : Fragment() {

    lateinit var binding: FragmentSearchBinding

    val viewModel: MainViewModel by navGraphViewModels(R.id.nav_main)

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentSearchBinding.inflate(inflater, container, false)
        binding.viewModel = viewModel
        binding.lifecycleOwner = requireActivity()
        initAdapter()
        return binding.root
    }

    private fun initAdapter() {
        val adapter = SearchFriendAdapter(mutableListOf(), parentFragmentManager)
        val layout = LinearLayoutManager(requireContext())
        viewModel.searchList.observe(requireActivity(), {
            adapter.changeList(it)
        })
        layout.orientation = LinearLayoutManager.VERTICAL
        binding.recyclerView2.layoutManager = layout
        binding.recyclerView2.adapter = adapter
    }
}